﻿using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.ServiceModel.DomainServices.EntityFramework;
using System.ServiceModel.DomainServices.Hosting;
using System.ServiceModel.DomainServices.Server;
using SilentWebsite.Web.Services.Contracts;

namespace SilentWebsite.Web.Services
{
    // Implements application logic using the SilentEntities context.
    // TODO: Add your application logic to these methods or in additional methods.
    // TODO: Wire up authentication (Windows/ASP.NET Forms) and uncomment the following to disable anonymous access
    // Also consider adding roles to restrict access as appropriate.
    // [RequiresAuthentication]
    [EnableClientAccess]
    public class SilentService : LinqToEntitiesDomainService<SilentEntities>, IAuthenticationService, IAccountService, IDataService
    {
        #region Implementation of IDataService

        [Invoke]
        public void AddProject(Project project)
        {
            if ((project.EntityState != EntityState.Detached))
                ObjectContext.ObjectStateManager.ChangeObjectState(project, EntityState.Added);
            else ObjectContext.Projects.AddObject(project);
            ObjectContext.SaveChanges();
        }
        [Invoke]
        public void UpdateProject(Project project)
        {
            ObjectContext.Projects.AttachAsModified(project, ChangeSet.GetOriginal(project));
            ObjectContext.SaveChanges();
        }
        [Invoke]
        public void DeleteProject(Project project)
        {
            if ((project.EntityState != EntityState.Detached))
                ObjectContext.ObjectStateManager.ChangeObjectState(project, EntityState.Deleted);
            else
            {
                ObjectContext.Projects.Attach(project);
                ObjectContext.Projects.DeleteObject(project);
            }
            ObjectContext.SaveChanges();
        }

        [Invoke]
        public void AddProjectPage(ProjectPage projectPage)
        {
            if ((projectPage.EntityState != EntityState.Detached))
            {
                ObjectContext.ObjectStateManager.ChangeObjectState(projectPage, EntityState.Added);
            }
            else
            {
                ObjectContext.ProjectPages.AddObject(projectPage);
            }
        }
        [Invoke]
        public void UpdateProjectPage(ProjectPage currentProjectPage)
        {
            ObjectContext.ProjectPages.AttachAsModified(currentProjectPage, ChangeSet.GetOriginal(currentProjectPage));
        }
        [Invoke]
        public void DeleteProjectPage(ProjectPage projectPage)
        {
            if ((projectPage.EntityState != EntityState.Detached))
            {
                ObjectContext.ObjectStateManager.ChangeObjectState(projectPage, EntityState.Deleted);
            }
            else
            {
                ObjectContext.ProjectPages.Attach(projectPage);
                ObjectContext.ProjectPages.DeleteObject(projectPage);
            }
        }

        public List<Project> GetProjects(long userId)
        {
            return ObjectContext.Projects.Where(x => x.UserId == userId).ToList();
        }
        public List<ProjectPage> GetProjectPages(long projectId)
        {
            return ObjectContext.ProjectPages.Where(x => x.ProjectId == projectId).ToList();
        }
        public List<DynamicControl> GetControls()
        {
            return ObjectContext.DynamicControls.ToList();
        }

        #endregion

        #region Implementation of IAccountServie

        [Invoke]
        public void CreateAccount(User user)
        {
            if ((user.EntityState != EntityState.Detached))
                ObjectContext.ObjectStateManager.ChangeObjectState(user, EntityState.Added);
            else ObjectContext.Users.AddObject(user);
            ObjectContext.SaveChanges();
        }
        [Invoke]
        public void UpdateAccount(User user)
        {
            ObjectContext.Users.AttachAsModified(user, ChangeSet.GetOriginal(user));
            ObjectContext.SaveChanges();
        }
        [Invoke]
        public void DeleteAccount(User user)
        {
            if ((user.EntityState != EntityState.Detached))
                ObjectContext.ObjectStateManager.ChangeObjectState(user, EntityState.Deleted);
            else
            {
                ObjectContext.Users.Attach(user);
                ObjectContext.Users.DeleteObject(user);
            }
            ObjectContext.SaveChanges();
        }

        #endregion

        #region Implementation of IAuthenticationService

        public User SignIn(string username, string password)
        {
            return ObjectContext.Users.FirstOrDefault(x => x.UserName == username && x.Password == password);
        }

        #endregion
    }
}


